package com.cuiyongzhi.wechat.menu;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.cuiyongzhi.web.util.GlobalConstants;
import com.cuiyongzhi.web.util.PropertiesUtil;
import com.cuiyongzhi.wechat.util.HttpUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class MenuMain {

	public static void main(String[] args) {
		Map<String, String> params = new HashMap<String, String>();
		//获取token执行体
		params.put("grant_type", "client_credential");
		params.put("appid", PropertiesUtil.getInstance().getProperty("appid"));
		params.put("secret", PropertiesUtil.getInstance().getProperty("AppSecret"));
		String jstoken = "";
		try {
			jstoken = HttpUtils.sendGet("https://api.weixin.qq.com/cgi-bin/token", params);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String access_token = JSONObject.fromObject(jstoken).getString("access_token"); // 获取到token并赋值保存
		
		System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+":token为=============================="+access_token);
		
		ViewButton one = new ViewButton();
		one.setUrl("https://4f2ac6b3.ngrok.io/wechat/main.html");
		one.setName("授权登录");
		one.setType("view");
		
		ViewButton three = new ViewButton();
		three.setUrl("http://www.detaoma.com");
		three.setName("教育官网");
		three.setType("view");
		
		//二级子菜单
		ClickButton cbt=new ClickButton();
		cbt.setKey("text");
		cbt.setName("回复图文");
		cbt.setType("click");
		
		ViewButton sub_one = new ViewButton();
		sub_one.setUrl("http://www.detaoma.com/about/guan-yu-wo-men.htm");
		sub_one.setName("徳稻教育");
		sub_one.setType("view");
		
		ViewButton sub_second = new ViewButton();
		sub_second.setUrl("http://www.detaoma.com/product/zhuan-ye-gong-jian.htm");
		sub_second.setName("教育产品");
		sub_second.setType("view");
		
		ViewButton sub_three = new ViewButton();
		sub_three.setUrl("http://www.detaoma.com/service/jiao-yu-yan-jiu.htm");
		sub_three.setName("教育服务");
		sub_three.setType("view");
		
		ViewButton sub_four = new ViewButton();
		sub_four.setUrl("http://www.detaoma.com/news/re-dian-xin-wen.htm");
		sub_four.setName("徳稻动态");
		sub_four.setType("view");
		
		JSONArray sub_button = new JSONArray();
		sub_button.add(cbt);
		sub_button.add(sub_one);
		sub_button.add(sub_second);
		sub_button.add(sub_three);
		sub_button.add(sub_four);
		
		JSONObject center = new JSONObject();
		center.put("name", "徳稻");
		center.put("sub_button", sub_button);
		
		//底部菜单
		JSONArray button=new JSONArray();
		button.add(one);
		button.add(center);
		button.add(three);
		
		JSONObject menujson=new JSONObject();
		menujson.put("button", button);
		System.out.println(menujson);
		//这里为请求接口的url   +号后面的是token，这里就不做过多对token获取的方法解释
		String url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+access_token;
		try{
			String rs=HttpUtils.sendPostBuffer(url, menujson.toString());
			System.out.println(rs);
		}catch(Exception e){
			System.out.println("请求错误！");
		}
	
	}

}
